/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package othello.UI.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import othello.UI.CustomJPanel;
import othello.logic.Computer;
import othello.logic.Computer_1;
import othello.logic.GameGrid;
import othello.logic.Human;
import othello.logic.Player;

/**
 *
 * @author Björn
 * En JDialog som tar in information om de deltagande spelarna.
 */
public class SetupGameDialog extends JDialog implements ActionListener {
    
    private Player player1;
    private Player player2;
    private String[] petStrings = {"Human", "Computer a", "Computer b"};
    private JComboBox jcmPlayer1 = new JComboBox(petStrings);
    private JComboBox jcmPlayer2 = new JComboBox(petStrings);
    private JTextField jtxName1 = new JTextField("Player 1");
    private JTextField jtxName2 = new JTextField("Player 2");
    private boolean testMode = false;
    private JButton jbtTest = new JButton("Test");
    private JButton jbtStart = new JButton("Start");
    private JLabel p1Avatar = new JLabel("");
    private JLabel p2Avatar = new JLabel("");
    
    public SetupGameDialog(JFrame owner) {
        super(owner, true);
        
        CustomJPanel setupPanel = new CustomJPanel("gfx/dialogbakgrund.png");
        JPanel player1Input = new JPanel(new GridLayout(3, 1, 10, 10));
        JPanel player2Input = new JPanel(new GridLayout(3, 1, 10, 10));
        JPanel jpnButtons = new JPanel(new GridLayout(3, 1, 10, 10));
        player1Input.setOpaque(false);
        player2Input.setOpaque(false);
        jpnButtons.setOpaque(false);
        setTitle("Othello");
        setResizable(false);
        
        jbtStart.addActionListener(this);
        jbtTest.addActionListener(this);
        
        jbtTest.setEnabled(false);
        JPanel jpnSetup = new JPanel(new GridLayout(3, 3, 10, 0));
        
        jpnSetup.add(new JLabel(""));
        jpnSetup.add(new JLabel(""));
        jpnSetup.add(new JLabel(""));
        
        player1Input.add(jtxName1);
        player1Input.add(jcmPlayer1);
        
        player2Input.add(jtxName2);
        player2Input.add(jcmPlayer2);
        
        jpnButtons.add(jbtStart);
        jpnButtons.add(jbtTest);
        
        p1Avatar = new JLabel("");
        p1Avatar.setVerticalAlignment(3);
        p1Avatar.setHorizontalAlignment(4);
        p1Avatar.setIcon(new ImageIcon("gfx/apaL.png"));
        
        p2Avatar = new JLabel("");
        p2Avatar.setVerticalAlignment(3);
        p2Avatar.setHorizontalAlignment(2);
        p2Avatar.setIcon(new ImageIcon("gfx/apaR.png"));
        
        jpnSetup.add(p1Avatar);
        jpnSetup.add(new JLabel(""));
        jpnSetup.add(p2Avatar);
        
        jpnSetup.add(player1Input);
        jpnSetup.add(jpnButtons);
        jpnSetup.add(player2Input);
        
        jcmPlayer1.addActionListener(this);
        jcmPlayer2.addActionListener(this);
        
        addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        jpnSetup.setOpaque(false);
        
        setupPanel.add(jpnSetup);
        
        getContentPane().add(setupPanel);
        pack();
        //centrerar dialogen
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width / 2 - this.getWidth() / 2, height / 2 - this.getHeight() / 2);
        
    }
    /*
     * Sätter spelarna utifrån comboboxarna.
     */
    private void setPlayers() {
        if (jcmPlayer1.getSelectedIndex() == 0) {
            player1 = new Human();
            player1.setName(jtxName1.getText());
            player1.setID(GameGrid.BLACK_MARKER);
        }
        if (jcmPlayer1.getSelectedIndex() == 1) {
            player1 = new Computer();
            player1.setName("P1 Computer A");
            player1.setID(GameGrid.BLACK_MARKER);
        }
        if (jcmPlayer1.getSelectedIndex() == 2) {
            player1 = new Computer_1();
            player1.setName("P1 Computer B");
            player1.setID(GameGrid.BLACK_MARKER);
        }
        
        if (jcmPlayer2.getSelectedIndex() == 0) {
            player2 = new Human();
            player2.setName(jtxName2.getText());
            player2.setID(GameGrid.WHITE_MARKER);
        }
        if (jcmPlayer2.getSelectedIndex() == 1) {
            player2 = new Computer();
            player2.setName("P2 Computer A");
            player2.setID(GameGrid.WHITE_MARKER);
        }
        if (jcmPlayer2.getSelectedIndex() == 2) {
            player2 = new Computer_1();
            player2.setName("P2 Computer B");
            player2.setID(GameGrid.WHITE_MARKER);
        }
        
    }
    
    public Player getPlayer1() {
        return player1;
    }
    
    public Player getPlayer2() {
        return player2;
    }
    
    public boolean getTestMode() {
        return testMode;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        if ("Start".equals(e.getActionCommand())) {
            setPlayers();
            this.setVisible(false);
        }
        
        if ("Test".equals(e.getActionCommand())) {
            setPlayers();
            testMode = true;
            this.setVisible(false);
        }
        /*
         * Byter grafik på avatarerna.
         */
        if ("comboBoxChanged".equals(e.getActionCommand())) {
            
            if (jcmPlayer1.getSelectedIndex() != 0 && jcmPlayer2.getSelectedIndex() != 0) {
                jbtTest.setEnabled(true);
            } else {
                jbtTest.setEnabled(false);
            }
            
            if (jcmPlayer1.getSelectedIndex() != 0) {
                p1Avatar.setIcon(new ImageIcon("gfx/robotL.png"));
            }
            if (jcmPlayer1.getSelectedIndex() == 0) {
                p1Avatar.setIcon(new ImageIcon("gfx/apaL.png"));
            }
            if (jcmPlayer2.getSelectedIndex() != 0) {
                p2Avatar.setIcon(new ImageIcon("gfx/robotR.png"));
            }
            if (jcmPlayer2.getSelectedIndex() == 0) {
                p2Avatar.setIcon(new ImageIcon("gfx/apaR.png"));
            }
            
        }
    }
    /*
     * Slår på/av testläget(ai vs ai)
     */
    public void setTestMode(boolean b) {
        this.testMode = b;
    }
}
